package com.luolc.syllabustest.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.NumberPicker;

import com.luolc.syllabustest.R;

import java.util.ArrayList;

import butterknife.ButterKnife;

/**
 * Name:上课具体时间选择
 * Author: WK（wangkun@xjzhicheng.com)
 * Date: 2016-10-12
 * Describe: PLEASE TO FIXME...
 */
public class ClassTimeChoiceDialog extends AlertDialog {

    public static final String[] WEEKDAY_TITLES = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    private static final String[] CLASS_START_TITLES = {"第1节", "第2节", "第3节", "第4节", "第5节", "第6节", "第7节", "第8节", "第9节", "第10节"};
    private static final String[] CLASS_END_TITLES = {"到1节", "到2节", "到3节", "到4节", "到5节", "到6节", "到7节", "到8节", "到9节", "到10节"};

    private NumberPicker mNumberPickerWeek;
    private NumberPicker mNumberPickerStart;
    private NumberPicker mNumberPickerEnd;

    private String[] mWeekDayData;
    private String[] mClassStartData;
    private String[] mClassEndData;

    private int currentWeek;
    private int currentStart;
    private int currentEnd;

    private ImageButton mIBtnDelTime;
    private ImageButton mIBtnSaveTime;

    ArrayList<Integer> mChoiceDataSource = new ArrayList<>();

    private IGetChoiceDataSource mIGetChoiceDataSource;
    private SyllabusClassTimeInfoView.ITimeCheckListener mITimeCheckListener;

    //数据CallBack接口
    public interface IGetChoiceDataSource {
        void onGetData(ArrayList<Integer> data);
    }

    private int CACHE_start;//保存开始时间

    public ClassTimeChoiceDialog(Context context) {
        super(context);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_dialog_time);
        ButterKnife.bind(this);
        initView();
        setListener();
        onInvalidateUI();
    }


    private void initView() {
        //默认初始化
        mWeekDayData = WEEKDAY_TITLES;
        mClassStartData = CLASS_START_TITLES;
        mClassEndData = CLASS_END_TITLES;
        //find view
        mNumberPickerWeek = (NumberPicker) findViewById(R.id.number_picker_week);
        mNumberPickerStart = (NumberPicker) findViewById(R.id.number_picker_start);
        mNumberPickerEnd = (NumberPicker) findViewById(R.id.number_picker_end);
        mIBtnDelTime = (ImageButton) findViewById(R.id.ibtn_del_time);
        mIBtnSaveTime = (ImageButton) findViewById(R.id.ibtn_save_time);
        //设置属性
        mNumberPickerWeek.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        mNumberPickerWeek.setMaxValue(WEEKDAY_TITLES.length - 1);
        mNumberPickerWeek.setMinValue(0);

        mNumberPickerStart.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        mNumberPickerStart.setMaxValue(CLASS_START_TITLES.length - 1);
        mNumberPickerStart.setMinValue(0);

        mNumberPickerEnd.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        mNumberPickerEnd.setMaxValue(CLASS_END_TITLES.length - 1);
        mNumberPickerEnd.setMinValue(0);
        //初始化数据
        mNumberPickerWeek.setDisplayedValues(mWeekDayData);
        mNumberPickerStart.setDisplayedValues(mClassStartData);
        mNumberPickerEnd.setDisplayedValues(mClassEndData);
    }


    private void setListener() {
        mNumberPickerWeek.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            }
        });
        mNumberPickerStart.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                CACHE_start = picker.getValue();
                mNumberPickerEnd.setValue(CACHE_start);
            }
        });
        mNumberPickerEnd.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int current = picker.getValue();
                if (current < CACHE_start) {
                    picker.setValue(CACHE_start);
                }
            }
        });

        // 取消事件
        mIBtnDelTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClassTimeChoiceDialog.this.dismiss();
            }
        });
        // 保存事件
        mIBtnSaveTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChoiceDataSource.clear();
                mChoiceDataSource.add(mNumberPickerWeek.getValue());
                mChoiceDataSource.add(mNumberPickerStart.getValue() + 1);
                mChoiceDataSource.add(mNumberPickerEnd.getValue() + 1);
                mIGetChoiceDataSource.onGetData(mChoiceDataSource);
                //TODO 要去做ClassTimeConflict验证..
                mITimeCheckListener.onCheck();
                //处理选择数据源
                ClassTimeChoiceDialog.this.dismiss();
            }
        });
    }

    private void onInvalidateUI() {
        mNumberPickerWeek.setValue(currentWeek);
        mNumberPickerStart.setValue(currentStart);
        mNumberPickerEnd.setValue(currentEnd);
    }

    /**
     * ex:周日 1-5
     * tip:"节"写在String ResID中
     *
     * @param data
     */
    public void setDataSource(ArrayList<Integer> data) {
        if (data == null) return;
        int size = data.size();
        if (size == 0) {
            this.currentWeek = 0;
            this.currentStart = 0;
            this.currentEnd = 0;
            return;
        } else if (size > 0) {
            this.currentWeek = data.get(0);
            this.currentStart = data.get(1) - 1;
            this.currentEnd = data.get(2) - 1;
        }
        onInvalidateUI();
    }

    @Override
    public void show() {
        super.show();
    }


    //---------------------------------------Getter,Setter------------------------------------------


    public String[] getWeekDayData() {
        return mWeekDayData;
    }

    public void setWeekDayData(String[] mWeekDayData) {
        this.mWeekDayData = mWeekDayData;
    }

    public String[] getClassStartData() {
        return mClassStartData;
    }

    public void setClassStartData(String[] mClassStartData) {
        this.mClassStartData = mClassStartData;
    }

    public String[] getClassEndData() {
        return mClassEndData;
    }

    public void setClassEndData(String[] mClassEndData) {
        this.mClassEndData = mClassEndData;
    }

    public void setIGetChoiceDataSourceListener(IGetChoiceDataSource mIGetChoiceDataSource) {
        this.mIGetChoiceDataSource = mIGetChoiceDataSource;
    }

    public void setTimeConflictCheckListener(SyllabusClassTimeInfoView.ITimeCheckListener timeConflictCheck) {
        this.mITimeCheckListener = timeConflictCheck;
    }

    public void setChoiceDataSource(ArrayList<Integer> choiceDataSource) {
        this.mChoiceDataSource = choiceDataSource;
    }

    public ArrayList<Integer> getChoiceDataSource() {
        return mChoiceDataSource;
    }

}
